package com.youkeda.dewu.api;

import com.youkeda.dewu.model.*;
import com.youkeda.dewu.param.QueryOrderParam;
import com.youkeda.dewu.service.OrderService;
import com.youkeda.dewu.service.ProductDetailService;
import com.youkeda.dewu.service.ProductService;
import com.youkeda.dewu.service.UserService;
import jodd.util.CollectionUtil;
import org.bouncycastle.util.io.pem.PemObject;
import org.graalvm.util.CollectionsUtil;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName OrderApi.java
 * @Description TODO
 * @createTime 2020年10月09日 12:52:00
 */
@RestController
@RequestMapping("/api/order")
public class OrderApi {
    // 通知消息格式
    private static final String MAG_TMPL = "%s 下单 %s";

    @Autowired
    private OrderService orderService;

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @Autowired
    private UserService userService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductDetailService productDetailService;

    @PostMapping("/add")
    public Result<Order> add(Order order, HttpServletRequest request){
           Result<Order> result = new Result<>();
           result.setSuccess(true);
        if (order == null){
            result.setSuccess(false);
            result.setMessage("order is null");
            return result;
        }
           Long userId = (Long)request.getSession().getAttribute("userId");
           if (userId == null){
               result.setSuccess(false);
               result.setMessage("userId is null");
               return result;
           }
           order.setUserId( userId);
           Order retrunOrder = orderService.add(order);

        CompletableFuture.supplyAsync(()->this.sendNotify(userId,order.getProductDetailId())).thenAccept(
                System.out::println
        );

           this.sendNotify(userId,order.getProductDetailId());

           result.setData(retrunOrder);
           return result;
    }
    @GetMapping("/queryrecentpaysuccess")
    public Result<Paging<Order>> queryRecentpaySuccess(QueryOrderParam param){
        Result<Paging<Order>> result = new Result<>();
        result.setSuccess(true);
        if (param == null){
            result.setSuccess(false);
            result.setMessage("param is null");
            return result;
        }
        Paging<Order> orderPaging = orderService.queryRecentPaySuccess(param);
        if (orderPaging == null){
            result.setSuccess(false);
            result.setMessage("orderPaging is null");
            return result;
        }
        result.setData(orderPaging);
        result.setMessage("successful");
        return result;
    }

    public String sendNotify(Long userId ,String productDetailId){
        if (null == userId || null == productDetailId) {

             return "error";
        }
        List<Long> userIds = new ArrayList<>();

        List<User> users = userService.queryUser(userIds);

        User buyer = null;

        if (!CollectionUtils.isEmpty(users)){
            buyer = users.get(0);
        }

        ProductDetail productDetail = productDetailService.getById(productDetailId);

        Product product = productService.get(productDetail.getProductId());


        String msg = String.format(MAG_TMPL, buyer.getUserName(), product.getName());

        kafkaTemplate.send("dewuNotify",msg);
        return "Send msg complete";

    }
}
